<template>
    <div>
        <Card>
            <!-- search start -->
            <div class="search">
                <div class="add">
                    <RadioGroup :value="'M'" type="button" @on-change="handleChange">
                        <Radio label="D">天</Radio>
                        <Radio label="M">月</Radio>
                        <Radio label="S">季</Radio>
                    </RadioGroup>
                </div>
                <Form :model="search" class="search_box" inline label-position="right">
                    <FormItem label="部门：">
                        <Select class="option" multiple v-model="search.department" @on-change=" selectDem ">
                            <Option value="全部">全部</Option>
                            <Option v-for="item in demList " :value="item" :key="item">{{ item }}</Option>
                        </Select>
                    </FormItem>

                    <FormItem label="日期：">
                        <DatePicker :value="time" style="width:300px" type="datetimerange" placeholder="Select date and time" @on-change="changeTime($event)"></DatePicker>
                    </FormItem>

                    <FormItem>
                        <Button icon="md-search" type="primary" @click="searchList">搜索</Button>
                        <Button v-if="role < 3" style="margin-left:10px" icon="ios-download-outline" type="success" @click="exportExcel">导出Excel</Button>
                    </FormItem>

                </Form>

            </div> <!-- end search -->

            <Table ref="table" :loading="loading" show-summary stripe :columns="columns1" :data="data1"></Table>
        </Card>
    </div>
</template>

<script>
    import axios from '@/libs/api.request'
    import {
        selectDepartment,
        selectTimeBtn
    } from '@/libs/tools'
    import {
        exportExcel
    } from '@/api/data'
    import {
        mapGetters
    } from 'vuex'
    export default {
        name: 'journal-page-all',
        data() {
            return {
                loading: false,

                time: [],
                // 搜索对象
                search: {
                    start_time: '',
                    end_time: '',
                    department: ['全部'],
                },

                columns1: [{
                        type: 'index',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '部门',
                        align: 'center',
                        key: 'department',
                    },
                    {
                        title: '收入',
                        align: 'center',
                        key: 'income_money',
                    },
                    {
                        title: '支出',
                        align: 'center',
                        key: 'expend_money',
                    }

                ],
                data1: []
            }
        },
        methods: {
            ...mapGetters(['getDem', 'getRole']),

            // 获取统计日记账列表
            getList() {
                this.loading = true
                let data = this.search
                axios.ajax_post('admin-work_record_account_total-ajax_get_wrat', data).then(res => {
                    this.loading = false
                    if (res.data.code === 1) {
                        this.data1 = res.data.data.list
                    } else {
                        this.$Notice.error({
                            title: res.data.msg,
                            duration: 3
                        })
                    }
                })

            },

            // 搜索
            searchList() {
                this.getList()
            },

            // 选择部门
            selectDem(value) {
                this.search.department = selectDepartment(value)
            },

            // 点击年月季
            handleChange(e) {
                let itemArr = selectTimeBtn(e)
                this.time = itemArr;
                [this.search.start_time, this.search.end_time] = itemArr
                this.getList()
            },

            // 初始化
            init() {
                this.handleChange('M')
            },

            // 导出
            exportExcel() {
                let {
                    ...out
                } = this.search
                out.department = JSON.stringify(out.department)
                exportExcel('admin-work_record_account_total-export_wrat', out, false)
            },

            // 格式化时间
            changeTime(value) {
                this.search.start_time = value[0]
                this.search.end_time = value[1]
            },

        },
        computed: {
            demList() {
                return this.getDem()
            },
            role() {
                return this.getRole()
            }
        },
        created() {
            this.init()
        }
    }
</script>


<style lang='less' scoped>
    .search {
        display: flex;
        justify-content: space-between;

        .add {
            margin-right: 20px;
            display: inline-block;
        }

        .search_box {
            width: auto;
            display: flex;
            flex-wrap: wrap;

            .option {
                width: auto;
            }

            .ivu-form-item {
                margin-right: 12px;
                display: flex;
                justify-content: flex-start;
                margin-bottom: 20px;
            }
        }
    }
</style>